<template>
  <div class="container">
    <el-dialog
      width="400px"
      :title="title"
      :visible="showDialog"
      @close="cancelClick"
    >
      <el-form ref="formData" :rules="formRules" :model="formdata" label-width="80px">
        <el-form-item v-if="!$route.query.id" prop="subjectID" label="所属学科">
          <el-select  v-model="formdata.subjectID" clearable size="mini" style="width: 100%" placeholder="请选择">
            <el-option v-for="item in subjectList" :key="item.value" :label="item.label" :value="item.value" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="directoryName" label="目录名称">
          <el-input  v-model="formdata.directoryName" size="mini" style="width: 100%" />
        </el-form-item>

        <el-form-item>
          <el-row type="flex" justify="end">
            <el-col :span="12" style="margin-top: 20px">
              <el-button size="mini" @click="cancelClick">取消</el-button>
              <el-button type="primary" size="mini" @click="addSure">确定</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { add, update, detail } from '@/api/hmmm/directorys.js'
import { simple } from '@/api/hmmm/subjects.js'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
    // currentID: {
    //   type: Number,
    //   default: null
    // }
  },
  data () {
    return {
      // 数据绑定
      formdata: {
        subjectID: null,
        directoryName: ''
      },
      // 校验
      formRules: {
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      },
      // 学科列表数据
      subjectList: []
    }
  },
  computed: {
    title () {
      return this.formdata.id ? '修改目录' : '新增目录'
    }
  },
  created () {
    this.getSubjectList()
  },
  methods: {
    // 获取标签详情
    async getTagsDetail (row) {
      const { data } = await detail(row)
      console.log(data)
      this.formdata = data
    },
    // 获取学科简单列表数据
    async getSubjectList () {
      const { data } = await simple()
      console.log(data)
      this.subjectList = data
    },
    // 取消关闭按钮
    cancelClick () {
      this.formdata = {
        subjectID: '',
        directoryName: ''
      }
      // this.$nextTick(() => {
      // this.currentID = null
      // })
      this.$refs.formData.resetFields()
      // 通知父组件关闭弹层
      this.$emit('update:showDialog', false)
    },
    // 确定按钮校验数据并提交
    addSure () {
      this.$refs.formData.validate(async (isOK) => {
        if (isOK) {
          try {
            if (this.formdata.id) {
              // 编辑
              await update(this.formdata)
            } else {
              if (this.$route.query.id) {
                this.formdata.subjectID = +this.$route.query.id
              }
              await add(this.formdata)
              this.$message.success('新增目录成功')
            }
            // 通知父组件动手
            this.$emit('updateDepartmentList')
            this.cancelClick()
          } catch (e) {
            this.$message.error('新增目录失败')
          }
        }
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
